import baseAxios from '../../config/axios';

const state = {
    warningList: [], // 报警列表.
    stateData: [{
            DEVICE_STATUS: 1,
            COUNT: 0,
            name: '设备在线',
            color: '#009944'
        },
        {
            DEVICE_STATUS: 2,
            COUNT: 0,
            name: '设备离线',
            color: '#ffffff'
        }
    ],
    detailNumber: {
        detailByDay: 5,
        detailByHistory: 30,
        alarmByDay: 22, // 当日 告警
        alarmBySerious: 2, // 严重告警
        alarmByUrgent: 1, // 紧急告警
    },
    mapButtonList: getMapButtonList(),
    fireInfo: {
        show: false,
        name: '',
        address: '',
        top: 0,
        left: 0
    }
};

const getters = {
    warningList: state => state.warningList,
    allData: state => {
        let sum = 0;
        state.stateData.forEach(item => {
            sum += item.COUNT;
        });
        return {
            DEVICE_STATUS: -1,
            COUNT: sum,
            name: '设备总数',
            color: '#009944'
        };
    },
    stateDataList: (state, getters) => {
        let arr = [getters.allData, ...state.stateData];
        return arr;
    },
    mapButtonList: state => state.mapButtonList,
    detailByMonth: state => state.detailNumber.detailByDay + state.detailNumber.detailByHistory,
    detailNumber: (state, getters) => {
        return {
            ...state.detailNumber,
            detailByMonth: getters.detailByMonth
        }
    },
    // 风险分析是否显示.
    riskShow: state => {
        var riskItem = state.mapButtonList[state.mapButtonList.length - 2];
        return riskItem.active;
    },
    fireInfo: state => state.fireInfo
};

const mutations = {
    addWarningItem(state, item) {
        state.warningList.push(item);
    },
    // 修改数量.
    setStateDataCount(state, stateItem) {
        let item = state.stateData.find(t => {
            return t.DEVICE_STATUS === stateItem.DEVICE_STATUS;
        });
        if (item) {
            item.COUNT = stateItem.COUNT;
        }
    },
    //地图按钮点击
    clickMapButton(state, item) {
        let flag = item.key <= 6
        let flg = !item.active || flag;
        clearMapButtonActive(flag);
        item.active = flg;
    },
    //清空设备在线离线数量.
    clearStatusCount(state) {
        state.stateData.forEach(element => {
            element.COUNT = 0;
        });
    },
    addStatusCount(state, item) {
        let model = state.stateData.find(t => t.DEVICE_STATUS == item.DEVICE_STATUS);
        if (!model) {
            if (item.DEVICE_STATUS != 3) {
                console.warn('设备状态未找到', item);
            }
            return;
        }
        model.COUNT += parseInt(item.COUNT, 10);
    },
    // 重置报警list
    setWarningList(state, list) {
        state.warningList = list;
    },
    // 修改火警浮动框数据
    changeFireInfo(state, info) {
        state.fireInfo = { 
            ...state.fireInfo,
            ...info
        };
    }
};

const actions = {
    //增加警告信息.
    addWarningItem(context, item) {
        context.commit('addWarningItem', item);
    },
    //清空在线离线.
    clearStatusCount(context) {
        context.commit('clearStatusCount');
    },
    //设备在线离线增加.
    addStatusCount(context, arr) {
        arr.forEach(item => {
            context.commit('addStatusCount', item);
        });
    },
    // 获取报警列表
    getWarningList(context) {
        return new Promise((resolve, reject) => {
            let url = 'pubinfo-restful/tdIotDevice/hiddenDangerSummary.rf';
            console.log(url);
            baseAxios.get(url).then(response => {
                let data = response.data.data;
                let list = data.pageMapList.map(t => {
                    let time = window.moment(t.happenedTime).format("HH:mm:ss");
                    return {
                        key: t.deviceId,
                        type: t.deviceBigName,
                        name: t.companyName + "_" + time,
                        ...t
                    }
                });
                context.commit('setWarningList', list);
                resolve(data);
            });
        });
    }
};

export default {
    state,
    getters,
    actions,
    mutations
};

function clearMapButtonActive(flag) {

    for (let i = 0; i < state.mapButtonList.length; i++) {
        const item = state.mapButtonList[i];
        if (flag || item.key <= 6) {
            item.active = false;
        }
    }

}

function getMapButtonList() {
    let arr = [];
    const temp = '全部,智慧用水,智慧用电,独立烟感,可燃气体,远程监控,人口密度,火警分析,四色预警,巡查统计'.split(',');
    for (let i = 0; i < temp.length; i++) {
        const st = temp[i];
        let obj = {
            key: i + 1,
            name: st,
            active: i === 0
        }
        arr.push(obj);
    }
    return arr;
}

// 模拟告警列表.
// function getWarningList() {
//     let arr = [];
//     for (let i = 0; i < 50; i++) {
//         let obj = {
//             key: i + 1,
//             type: '智慧用电',
//             name: i + '温州茂铭眼镜厂温州茂铭眼镜厂温州茂铭眼镜厂_16:58:25'
//         }
//         arr.push(obj);
//     }
//     return arr;
// }
